window.alert(
  "本作品的哲学是将注意力集中到刷题上而不用浪费时间一个个去按照现有正确答案去点"
);
if (
  window.confirm(
    "我已经深刻考虑到使用本脚本只为了赶上deadline是对自己不负责任的行为，也明白这是作者不愿意看到的"
  )
) {
  ((window) => {
    try {
      const lenOfQuestion = document
        .getElementsByClassName("exam-main")[0]
        .getElementsByClassName("subject-item").length;
      const getItemCheckStates = (questionIndex) => {
        return Array.from(
          document
            .getElementsByClassName("exam-main")[0]
            .getElementsByClassName("subject-item")
            [questionIndex].getElementsByTagName("label")
        ).map((item) => {
          return Array.from(item.classList).some(
            (item) => item === "is-checked"
          );
        });
      };
      const changeItemCheckState = (questionIndex, optionIndex) => {
        document
          .getElementsByClassName("exam-main")[0]
          .getElementsByClassName("subject-item")
          [questionIndex].getElementsByTagName("label")
          [optionIndex].click();
      };
      const getQuestionType = (questionIndex) => {
        return (
          document
            .getElementsByClassName("exam-main")[0]
            .getElementsByClassName("subject-item")
            [questionIndex].getElementsByTagName("label")[0]
            ?.classList[0].split('-')[1] ?? "other"
        );
      };
      const myMessage = (message, isShowTime) => {
        responesArea.innerText =
          (isShowTime ? new Date().toLocaleString() + ": " : "") + message;
      };
      const contaienr = window.document.createElement("div");
      with (contaienr.style)
        (width = "400px"),
          (position = "fixed"),
          (bottom = "5px"),
          (right = "5px"),
          (zIndex = "999"),
          (boxSizing = "border-box");
      const topTool = window.document.createElement("div");
      with (((topTool.innerText = "rainTestHelper"), topTool.style))
        (textAlign = "center"),
          (lineHeight = "20px"),
          (height = "20px"),
          (width = "100%"),
          (color = "rgb(97,175,233)"),
          (fontSize = "14px");
      const collapseButton = window.document.createElement("button");
      with (((collapseButton.innerText = "收起"), collapseButton.style))
        (float = "right"),
          (width = "40px"),
          (height = "20px"),
          (border = "none"),
          (cursor = "pointer"),
          (backgroundColor = "#1890ff"),
          (borderRadius = "0"),
          (color = "#ffffff");
      const mainWindow = window.document.createElement("div");
      with (mainWindow.style)
        (width = "100%"),
          (backgroundColor = "rgba(97,175,233, .5)"),
          (padding = "10px"),
          (boxSizing = "border-box");
      const textArea = window.document.createElement("textarea");
      with (((textArea.placeholder = "请在此处粘贴代码"), textArea.style))
        (boxSizing = "border-box"),
          (width = "100%"),
          (height = "100px"),
          (resize = "none"),
          (outline = "none"),
          (background = "rgba(255,255,255,.5)");
      const buttonArea = window.document.createElement("div");
      with (buttonArea.style)
        (width = "100%"),
          (boxSizing = "border-box"),
          (height = "30px"),
          (display = "flex");
      const button1 = window.document.createElement("button");
      with (((button1.innerText = "导入外部状态"), button1.style))
        (width = "max-content"),
          (height = "28px"),
          (padding = "0 5px"),
          (marginLeft = "5px");
      const button2 = window.document.createElement("button");
      with (((button2.innerText = "导出当前状态"), button2.style))
        (width = "max-content"),
          (padding = "0 5px"),
          (height = "28px"),
          (marginLeft = "5px");
      const responesArea = window.document.createElement("div");
      with (responesArea.style)
        (boxSizing = "border-box"),
          (width = "100%"),
          (minHeight = "50px"),
          (maxHeight = "200px"),
          (marginTop = "5px"),
          (overflow = "auto"),
          (position = "relative"),
          (backgroundColor = "rgba(255,255,255,.5)");
      collapseButton.addEventListener(
        "click",
        () => {
          if (collapseButton.innerText === "展开") {
            mainWindow.style.display = "block";
            collapseButton.innerText = "收起";
            return;
          }
          if (collapseButton.innerText === "收起") {
            mainWindow.style.display = "none";
            collapseButton.innerText = "展开";
            return;
          }
        },
        false
      );
      button1.addEventListener(
        "click",
        () => {
          if (textArea.value === "") {
            window.alert("您还没有输入代码");
            return;
          }
          if (!window.confirm("强烈建议导入前先导出做好备份，是否继续？")) {
            return;
          }
          const dict = JSON.parse(textArea.value);
          const trace = [];
          let count = 0;
          for (let i = 0; i < lenOfQuestion; i++) {
            const itemCheckStates = getItemCheckStates(i);
            if (dict[i].type === "radio") {
              dict[i].state.forEach((item, index) => {
                if (item == "1") {
                  let timer = setTimeout(() => {
                    myMessage("处理中" + (i + 1) + "/" + lenOfQuestion, false);
                    changeItemCheckState(i, index);
                    trace.push([i, index, 0, 1]);
                    trace.push([i, dict[i].state[index], 1, 0]);
                    clearTimeout(timer);
                  }, 500 * count++);
                  return;
                }
              });
              break;
            }
            if (dict[i].type === "checkbox") {
              dict[i].state.forEach((item, index) => {
                if (item == "1" && !itemCheckStates[index]) {
                  let timer = setTimeout(() => {
                    myMessage("处理中" + (i + 1) + "/" + lenOfQuestion, false);
                    changeItemCheckState(i, index);
                    trace.push([i, index, 0, 1]);
                    clearTimeout(timer);
                  }, 500 * count++);
                  return;
                }
                if (item == "0" && itemCheckStates[index]) {
                  let timer = setTimeout(() => {
                    myMessage("处理中" + (i + 1) + "/" + lenOfQuestion, false);
                    changeItemCheckState(i, index);
                    trace.push([i, index, 1, 0]);
                    clearTimeout(timer);
                  }, 500 * count++);
                  return;
                }
              });
              break;
            }
          }
          setTimeout(() => {
            let traceString = "";
            trace.forEach((item) => {
              traceString += `第${item[0] + 1}题: 第${item[1] + 1}个选项由${
                item[2] == "1" ? "选中" : "取消"
              }变为${item[3] == "1" ? "选中" : "取消"}\n`;
            });
            myMessage(
              "选择题和判断题处理完成，改动如下:\n" + traceString,
              true
            );
          }, 500 * count);
        },
        false
      );
      button2.addEventListener(
        "click",
        () => {
          let temp = [];
          for (let i = 0; i < lenOfQuestion; i++) {
            temp.push({
              state: getItemCheckStates(i).map((item) => Number(item)),
              type: getQuestionType(i),
            });
          }
          myMessage(JSON.stringify(temp), false);
          temp = null;
        },
        false
      );
      topTool.appendChild(collapseButton),
        contaienr.appendChild(topTool),
        mainWindow.appendChild(textArea),
        buttonArea.appendChild(button1),
        buttonArea.appendChild(button2),
        mainWindow.appendChild(buttonArea),
        mainWindow.appendChild(responesArea),
        contaienr.appendChild(mainWindow),
        window.document.body.appendChild(contaienr);
    } catch (e) {
      window.alert(
        "gachi helper: 发生未知错误，如果您能截图下面内容并联系qq1789551634, 作者将给第一个报告这个问题的人一个小红包~\n" +
          e
      );
    }
  })(window);
}
window.alert(
  "再啰嗦一句，掌握扎扎实实的基础知识，秉承终身学习的精神，你才不会被你室友的工资馋哭~"
);
